<?php
if (!defined('IN_ZWS')) exit();

class UserAction extends PublicAction {
	function index() {
		header('Location:' . U('all'));
	} 

	function all() {
		$user = D('User');
		$count = array('users' => $user -> count(),
			'admin' => count(array_filter(explode(',', setting('admin_uid')))),
			);
		import('ORG.Util.Page');
		$page = new Page($count['users'], 10);
		$show = $page -> show();
		$userList = $user -> relation(true) -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> assign('userList', $userList);
		$this -> display();
	} 

	function admin() {
		$admin_uid = explode(',', setting('admin_uid'));
		$count = count($admin_uid);
		$user = D('User');
		foreach($admin_uid as $uid) {
			$sqlArray[] = 'id=' . $uid;
		} 
		if (!empty($sqlArray)) {
			import('ORG.Util.Page');
			$page = new Page($count, 10);
			$show = $page -> show();
			$adminInfo = $user -> relation(true) -> where(implode(' OR ', $sqlArray)) -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		} 
		$this -> assign('adminInfo', $adminInfo);
		$this -> assign('page', $show);
		$this -> assign('count', $count);
		$this -> display();
	} 

	function verify() {
		$verify = M('UserVerify');
		$count = $verify -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$userVerify = $verify -> order('id desc') -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> assign('userVerify', $userVerify);
		$this -> display();
	} 

	function doVerify() {
		$type = intval($this -> _get('type'));
		$id = intval($this -> _get('id'));
		if (empty($type) || empty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$verify = M('UserVerify');
		$verifyInfo = $verify -> where('id=' . $id) -> find();
		if (empty($verifyInfo)) $this -> ajaxReturn('', '没有该记录！', 1);
		switch ($type) {
			case 1:
				$data = json_decode($verifyInfo['data'], true);
				$data ['username'] = $verifyInfo ['username'];
				$data ['email'] = $verifyInfo ['email'];
				$data['info'] = array('sex' => 0);
				$data['credit'] = array('credit' => 100);
				$user = D('User');
				if ($user -> relation(true) -> add($data)) {
					$verify -> where ('id=' . $id) -> delete ();
					$this -> ajaxReturn('', '已经成功通过该记录！', 0);
				} else $this -> ajaxReturn('', '操作失败！', 1);
				break;
			case 2:
				import ('ORG.Util.String');
				$verifyInfo['authcode'] = String :: randString (64);
				$verifyInfo['time'] = time();
				if ($verify -> where('id=' . $id) -> save($verifyInfo) !== false) {
					sendVerifyMail($verifyInfo, 1);
					$this -> ajaxReturn('', '已经重发验证邮件', 0);
				} 
				$this -> ajaxReturn('', '操作失败！', 1);
				break;
			case 3:
				if ($verify -> delete($id)) $this -> ajaxReturn('', '已经删除该记录！', 0);
				else $this -> ajaxReturn('', '操作失败！', 1);
				break;
			default:
				$this -> ajaxReturn('', '非法操作！', 1);
		} 
	} 

	function log() {
		$slt = $this -> _get('slt');
		if (!empty($slt) && in_array($slt, array('uid'))) {
			$sql = array($slt => $this -> _get($slt));
			$this -> assign('slt', $slt);
		} else $sql = '1=1';
		$log = D('LogView');
		$count = $log -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$userLog = $log -> where($sql) -> order('id desc') -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> assign('userLog', $userLog);
		$this -> display();
	} 

	function delLog() {
		$id = intval($this -> _get('id'));
		if (empty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$log = M('UserLog');
		if ($log -> delete($id)) $this -> ajaxReturn('', '删除成功', 0);
		$this -> ajaxReturn('', '删除失败！', 1);
	} 

	function edit() {
		$user = D('InfoView');
		$userInfo = $user -> where('User.id=' . intval($this -> _get('id'))) -> find();
		if (isEmpty($userInfo)) $this -> error('该用户不存在！');
		$this -> assign('userInfo', $userInfo);
		$admin_uid = explode(',', setting('admin_uid'));
		$this -> assign('isAdmin', in_array($userInfo['id'], $admin_uid));
		$this -> display();
	} 

	function doEdit() {
		$id = intval($this -> _get('id'));
		if (!$id) $this -> ajaxReturn('', '非法操作！', 1);
		import('ORG.Util.String');
		$data = array('id' => $id,
			'username' => $this -> _post('username'),
			'email' => $this -> _post('email'),
			'info' => array('name' => $this -> _post('name'),
				'sex' => (intval($this -> _post('sex')) > 2 || intval($this -> _post('sex')) < 0) ? 0 : intval($this -> _post('sex')),
				'mobile' => $this -> _post('mobile'),
				'site' => $this -> _post('site'),
				'address' => $this -> _post('address'),
				'zip' => $this -> _post('zip'),
				'intro' => $this -> _post('Intro'),
				),
			'credit' => array('integration' => $this -> _post('integration'), 'credit' => $this -> _post('credit')),
			);
		$data = array_filter($data);
		$data['credit'] = $data['credit'];
		if (!isEmpty($this -> _post('password'))) {
			$data['salt'] = String :: randString();
			$data['password'] = md5(md5($this -> _post('password')) . $data['salt']);
			$data['cptime'] = time();
		} 
		$admin_uid = explode(',', setting('admin_uid'));
		for($i = 0;$i < count($admin_uid);$i++) {
			if ($admin_uid[$i] == $id) unset($admin_uid[$i]);
		} 
		if ($this -> _post('admin') == 1) $admin_uid[] = $id;
		setting('admin_uid', implode(',', $admin_uid));
		$user = D('User');
		if ($user -> relation(true) -> save($data) !== false) $this -> ajaxReturn(array('redirect' => U('edit?id=' . $id), 'delay' => 3000), '编辑成功，3秒后刷新本页面', 0);
		$this -> ajaxReturn('', '编辑失败', 1);
	} 

	function delUser() {
		$id = intval($this -> _get('id'));
		if ($id == $_SESSION['uid']) $this -> ajaxReturn('', '不能删除自己的帐号！', 1);
		$user = D('User');
		if ($user -> relation(true) -> delete($id)) $this -> ajaxReturn('', '删除成功', 0);
		$this -> ajaxReturn('', '删除失败', 1);
	} 

	function addUser() {
		$user = D('UserReg');
		if ($user -> create($this -> _post())) {
			$userR = $user -> getData();
			$userR['info'] = array('sex' => 1);
			$userR['credit'] = array('credit' => 100);
			$D = D('User');
			if ($D -> relation(true) -> add($userR)) $this -> ajaxReturn(array('redirect' => U('all'), 'delay' => 2000), '添加成功，2秒钟后刷新本页面', 0);
			$this -> ajaxReturn('', '添加失败', 1);
		} else {
			$this -> ajaxReturn('', $user -> getError(), 1);
		} 
	} 

	function addAdmin() {
		if (isEmpty($this -> _post('uid'))) $this -> ajaxReturn('', '请填写UID！', 1);
		$user = M('User');
		if (isEmpty($user -> where('id=' . $this -> _post('uid')) -> find())) $this -> ajaxReturn('', '没有该用户！', 1);
		$admin_uid = explode(',', setting('admin_uid'));
		for($i = 0;$i < count($admin_uid);$i++) {
			if ($admin_uid[$i] == $this -> _post('uid')) unset($admin_uid[$i]);
		} 
		$admin_uid[] = $this -> _post('uid');
		if (setting('admin_uid', implode(',', $admin_uid))) $this -> ajaxReturn('', '添加成功', 0);
		$this -> ajaxReturn('', '添加失败', 1);
	} 

	function delAdmin() {
		$id = $this -> _get('id');
		if (isEmpty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$admin_uid = explode(',', setting('admin_uid'));
		if (count($admin_uid) == 1) $this -> ajaxReturn('', '至少应有1个管理员！', 1);
		if (!in_array($id, $admin_uid)) $this -> ajaxReturn('', '该用户没有管理员权限，无需删除！', 1);
		for($i = 0;$i < count($admin_uid);$i++) {
			if ($admin_uid[$i] == $this -> _get('id')) unset($admin_uid[$i]);
		} 
		if (setting('admin_uid', implode(',', $admin_uid))) $this -> ajaxReturn('', '删除成功', 0);
	} 
} 
